簡單來說是存放一組資料集
let drinks = ['coffee','tea','juice'];
let price = [10,20,30,40];
直接以範例說明
要讀取 drinks 的第一個元素的資料,則寫法是 drinks[0]
因為陣列是從 0 開始,取得時寫法為 drinks[0]
let drinks = ['coffee','tea','juice'];
console.log(drinks[0]); // 結果為 coffee
console.log(drinks[1]); // 結果為 tea
let drinks = ['coffee','tea','juice'];
console.log(drinks.length); // 結果為 3
let drinks = ['coffee','tea','juice'];
let drinkNum = drinks.length;
console.log(drinkNum); // 結果為 3
let drinks = [];
drinks[0] = 'tea';
let drinks = [];
drinks[0] = 'tea';
drinks[2] = 'coffee';
let drinks = ['tea','coffee','juice'];
drinks.push('milk');
let drinks = ['tea','coffee','juice'];
drinks.unshift('milk');
console.log(drinks); // 結果為 ['milk','tea','coffee','juice']
let drinks = ['tea','coffee','juice'];
drinks.pop();
console.log(drinks); // 結果為 ['tea','coffee']
let drinks = ['tea','coffee','juice'];
drinks.shift();
console.log(drinks); // 結果為 ['coffee','juice']
let drinks = ['tea','coffee','juice','milk'];
drinks.splice(1,2); // 從第 1 個位置開始刪除 2 筆資料
console.log(drinks); // 結果為 ['tea','milk']
本篇是陣列的基本用法,對於資料的新增、修改、刪除
今天是 Day 4 ,繼續加油!
這是我的學習紀錄,也歡迎各路大神指點指教!
我們下回見~